use crate::utils::{Array, Video, VideoFormat};

pub fn videos(category: &str) -> Array<Video> {
    match reqwest::blocking::get(format!(
        "https://www.cgtn.com/subscribe/rss/section/{}.xml",
        category
    )) {
        Ok(response) => {
            if response.status().is_success() {
                let mut videos: Vec<Video> = Vec::new();

                match response
                    .text()
                    .and_then(|text| Ok(rss::Channel::read_from(text.as_bytes())))
                {
                    Ok(Ok(channel)) => {
                        for item in channel.items() {
                            match (
                                item.title(),
                                item.description(),
                                item.link(),
                                item.content(),
                            ) {
                                (Some(title), Some(description), Some(link), Some(content)) => {
                                    match tl::parse(content, tl::ParserOptions::default()) {
                                        Ok(dom) => {
                                            match dom
                                                .query_selector("video")
                                                .and_then(|mut iter| iter.next())
                                                .and_then(|handle| handle.get(dom.parser()))
                                                .and_then(|node| node.as_tag())
                                            {
                                                Some(tag) => {
                                                    match (
                                                        tag.attributes().get("src"),
                                                        tag.attributes().get("poster"),
                                                    ) {
                                                        (Some(Some(src)), Some(Some(poster))) => {
                                                            videos.push(Video {
                                                                title: title.into(),
                                                                description: description.into(),
                                                                url: link.into(),
                                                                format: VideoFormat::M3U8,
                                                                src: src.as_utf8_str().into(),
                                                                poster: poster.as_utf8_str().into(),
                                                            });
                                                        }
                                                        _ => {}
                                                    }
                                                }
                                                None => {}
                                            }
                                        }
                                        Err(_) => {}
                                    }
                                }
                                _ => {}
                            }
                        }
                    }
                    _ => {}
                }

                Array { items: videos }
            } else {
                Array { items: Vec::new() }
            }
        }
        Err(_) => Array { items: Vec::new() },
    }
}
